package com.ssyc.student.activity;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.student.R;
import com.ssyc.student.adapter.StTeamRvAdapter;
import com.ssyc.student.bean.StTeamInfo;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

/**
 * 学生端---群组详情的界面
 * Created by Administrator on 2018/5/22 0022.
 */

@Route(path = "/stduent/StudentTeamInfoActivity")
public class StudentTeamInfoActivity extends BaseActivity implements View.OnClickListener, SrfManager.OnPullRefreshListener {

    //获取携带过来的网易云的群组id
    private String groupId;
    private ImageView rlClose;
    private RecyclerView rv;
    private SmartRefreshLayout srf;
    private AVLoadingIndicatorView avi;
    private RelativeLayout rlLoading;
    private View headView;
    private View emptyView;
    //数据源
    private List<StTeamInfo.ListBean> oldDatas;
    private StTeamRvAdapter adapter;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }


    @Override
    protected String setTitleText() {
        return null;
    }


    @Override
    protected int getContentLayout() {
        return R.layout.student_activity_team_info;
    }


    @Override
    protected void init() {

        showContent();

        //初始化视图
        initView();

        //初始化下拉刷新控件
        initSrf();

        //初始化列表
        initRv();

        //请求页面数据
        http(BqaManager.NORML);
    }

    /**
     * 初始化下拉刷新控件
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }

    /**
     * 请求页面数据
     */
    private void http(int method) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "24");
        map.put("c", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("platform", "8");
        map.put("groupid", "");//gropid有就传,没有就传空串
        map.put("tid", groupId);
        HttpUtils.post(HttpAdress.TEADMDETAILS, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                BqaManager.setErrorNet(rlLoading, srf, method, adapter);
            }

            @Override
            public void onResponse(String response, int id) {

                if (null != rlLoading) {
                    rlLoading.setVisibility(View.GONE);
                }
                StTeamInfo info = null;
                if (!TextUtils.isEmpty(response)) {
                    try {
                        info = GsonUtil.jsonToBean(response, StTeamInfo.class);
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.i("test", "解析数据异常");
                    }
                    if (null != info) {
                        if ("1".equals(info.getState())) {
                            List<StTeamInfo.ListBean> newDatas = info.getList();

                            //获取群的相关信息,获取群主的名称
                            String bossName = "";
                            if (null != newDatas && newDatas.size() != 0) {
                                for (int i = 0; i < newDatas.size(); i++) {
                                    if ("2".equals(newDatas.get(i).getRole())) {
                                        bossName = newDatas.get(i).getName();
                                    }
                                }

                            }
                            setTeaminfo(info.getGroupicon(), bossName);

                            //设置列表的相关信息
                            if (method == BqaManager.NORML) {

                                oldDatas.addAll(newDatas);

                                BqaManager.setRv(emptyView, StudentTeamInfoActivity.this, adapter, rv, null);

                            } else if (method == BqaManager.PULLREFRESH) {
                                //下拉刷新
                                BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                                //这里还必须重新设置一次关闭加载更多的开关,仅仅适用于不需要加载更多功能的页面效果
                                adapter.setEnableLoadMore(false);
                            }
                        } else {
                            //返回错误
                            Log.i("test", info.getState() + "");
                        }
                    }
                }

            }
        });
    }

    /**
     * 初始化视图的方法
     */
    private void initView() {
        groupId = getIntent().getStringExtra("accid");
//        Log.i("test", "获取到的网易云群ID是:" + groupId);
        rlClose = findViewById(R.id.rl_close);
        rlClose.setOnClickListener(this);
        rv = findViewById(R.id.rv);
        srf = findViewById(R.id.srf);
        avi = findViewById(R.id.avi);
        rlLoading = findViewById(R.id.rl_loading);
        rlLoading.setVisibility(View.VISIBLE);
    }

    /**
     * 初始化列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        headView = View.inflate(this, R.layout.st_team_details_head, null);
        adapter = new StTeamRvAdapter(this, R.layout.student_rv_team_details, oldDatas);
        adapter.addHeaderView(headView);
    }

    /**
     * 设置头布局群头像和名称的方法
     *
     * @param groupicon 群头像
     * @param groupname 群主姓名
     */
    private void setTeaminfo(String groupicon, String groupname) {
        CircleImageView ivTeam = headView.findViewById(R.id.iv_head);
        TextView tvName = headView.findViewById(R.id.tv_name);
        ImageUtil.displayImage(this, ivTeam, groupicon);
        tvName.setText(groupname);
    }


    /**
     * 页面控件的点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rl_close) {
            //返回
            finish();
        }
    }

    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }
}
